package com.yanqu.road.server.cmd;

import com.yanqu.road.connect.AgentServerLinkControl;
import com.yanqu.road.define.YanQuMessageDefine;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.union.cross.CrossUnionProto;
import com.yanqu.road.player.Player;
import com.yanqu.road.player.PlayerControl;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import io.netty.channel.ChannelHandlerContext;

@Cmd(code = Protocol.A_CROSS_UNION_MEMBER_MESSAGE_PUSH, desc = "跨服发起，商会成员聊天推送")
public class UnionMemberChatSyncCmd implements Command {

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {

        CrossUnionProto.UnionMemberChatSyncMsg msg = CrossUnionProto.UnionMemberChatSyncMsg.parseFrom(packet.getBytes());
        for (long userId : msg.getUserIdList()) {
            Player player = PlayerControl.getOnlinePlayer(userId);
            if(player != null) {
                YanQuMessage message = YanQuMessageUtils.buildMessageByte(Protocol.U_WORLD_MESSAGE_SYNC, userId, msg.getMsg().toByteArray());
                message.setPlayerId(userId);
                message.setLen(msg.getMsg().toByteArray().length + YanQuMessageDefine.HEADER_SIZE);
                AgentServerLinkControl.routeClient(message);
            }
        }
    }
}
